Today, we're going to start adding points to our leaderboard and we're going to use the coin to do
that.
We'll also add points when we do NPC kills once we start making our weapons.
So let's go ahead and get started with that.
Let's go ahead and turn off our game stop and then we'll go to server storage.
Open that up if it's closed.
Check to see that you got a coin, because that's what we're going to spawn and we're going to spawn
it from server store to make sure it's here.
All right.
We did that earlier in the course.
So now let's go up to our workspace and we're going to add a folder.
And we're going to call that coins because we're going to put the coins in the folder much like we did
the health and the mines and all that other stuff.
Coins.
Cool.
Now go down to server scripts service.
If you don't have it open, I have it open, but I'll show you where to find it again and we'll get
session utils.
Scroll up to the top and we're going to put a.
A variable for our folder.
Say local coin folder.
Workspace coins.
That's where our coins are going to go.
Now the health and the coins are going to be very similar.
So you could be thinking, Hey, I could just modify this health to be a little more general.
Save some code.
That'd be a good idea.
You could do that as an exercise.
I'm just going to copy and paste it.
Right.
That's for the innovation sprint, right.
Refactoring your code.
All right.
So I read, I read, pasted our health, our place, health.
Let's change that to place coin.
Coins.
Let's do with an SE.
We're going to need the number of coins.
We're not going to need a center point because we're going to span the whole terrain.
But we will need the distance.
The distance is going to be the size of the terrain because you might want to have this on different
size terrains.
All right.
Now to health.
Well, we're going to go ahead and get a template coin that was for template health.
In server storage.
If you have your coin there, you should see coin.
There we go.
Make sure you change this.
This is your coin folder.
The number of elements in the coin folder.
There we go.
So we're going to get the coin folders, children.
And then this is going to give us the number of those children.
Elements, coins, a number of coins, and then we'll get our number of coins here.
If the count is insufficient, we are going to spawn more coins.
This looks good right here.
This is our distance.
We're going to get random x, a random z.
We're going to use our ray casting to get the height because we might have unusual terrain.
We're not going to use the center point.
So go ahead and get rid of that center point X plus.
Just put the x rand in there.
And this is going to default to the zero part of the world on the X and the Z to be the center point.
And that looks good to health.
Well, we're going to make a clone of our HT coin.
Cool.
This will be Coyne.
Let's go ahead and copy that coin.
Paste that here and do not forget this or you're going to crash your system because you're going to
you're going to be counting from your coin folder, not your health folder.
So keep making coins If you're putting them in your health folder, this is going to crash because we're
never going to hit our number of coins and you're going to have too many resources going into your coins
and put a coin position.
Time to live in the coin.
We make that.
Yeah, that's good.
5 minutes.
And the count.
While the health isn't going to be as many as the coin say, make sure you get that one right to coin
folder.
All right.
Do we miss anything?
This gets me all the time.
You really shouldn't copy and paste.
But I do it all the time.
Everybody does, right?
That's looking good.
Now we're going to call this place coins.
We can copy this so we don't forget Control C and now we'll go to our wave loop.
Here we go.
And we're going to need a couple of variables.
So we have local number of coins and let's make a bunch of them.
Let's make like 1000 coins.
Cool.
And then we can do distance.
From coin center, and that's like the center of the world, right?
So the distance outward from our coin center area, and we're going to make that 1024 because our terrain
is 2048 by 2048 zero being in the center.
So we can go plus or -1024.
Cool.
Let's go down here.
Utils.
Place coins so we didn't have to copy it.
It gave it to us.
And then we'll have a number of coins.
And then the dist.
Oh, the distance is the distance from coin center.
And I put a parentheses there.
That's looking good.
Let's go ahead and check it out.
Hit my play button.
Where in the world?
And let's check for errors.
Output.
No errors.
That's looking good.
There are some coins.
All right.
We don't have points yet, but we can get the coins and they disappear.
Looking good.
Let's check our workspace.
Make sure the coins are going in there.
Yeah, it's going to be like 1000 of them.
That may have been overkill, but you can adjust that number however you want.
I thought.
I thought lots of coins would be cool.
All right.
Let's go ahead and add points to our coins using the leaderboard.
All right, let's go to our server storage and then the coin in the server storage.
Let's click on the collect script.
Bring it on up.
All right.
So this is what we this is our touched event, right?
So the coin touched event is connected to on collect on collect is fired when somebody touches it,
when a character touches it, we check to see if there's a humanoid, if there is a humanoid to be given
an award.
We want to make sure that it's just players were giving the award to and now we're going to put points
on the player's leaderboard.
So the first thing we need to do is get the players service so we can get the player and then get the
leaderboard.
And I'm going to say game get service players.
Now we have the player service and that variable right there.
So we get to here.
Let's say we got here, there's a humanoid, so it's a character of some type.
It could be a zombie, it could be a player, but there's a character associated because we have a humanoid.
Let's check, let's say local player.
We're going to check for a player and we're going to do that by getting the player's service get player
from character.
Well, if Humanoid exists, their other part parent is definitely a character, just might not be a
player.
So then we'll say if player, then do all our cool stuff and put an end down here at the bottom and
then we'll go to format selection, format document.
Now it looks good.
All right.
So if we get here, we know we have a player.
What are we going to do?
We want to try and get points off the player's leaderboard.
All the players have a leaderboard.
We did that earlier.
Oh, I do that every time points.
And we've got the player and then the player has the leader stats.
Remember, we call that leader stats.
Got to call it leader stats and then the leader stats have points.
I use the bracket notation points.
So the name of the metric, the thing that you're going to see on the screen for the leaderboard.
All right, now we want to increase our points so we get the points, points that are an value.
So you have to do that value thing.
Then we're going to do a plus equals ten.
So we're going to take the old value and add ten to it.
That's a plus.
Equals means I should do it.
Let's go ahead and give it a try.
Hit the play button.
Let's check for Iyer's view.
Output.
Looking good.
No Ayers coins should be popping up at any second.
There's a coin.
So get our points.
Booyah.
We got ten points and we can run real fast.
We could already do that.
I missed the point.
Or missed the coin.
Cool.
That's looking good.
They were good to go.
And the next video, what I'm going to do is let me turn that off.
And the next video I'm going to do is add a sound and then have a little gooey, a little gooey.
Go up saying that you got awarded 1010 gold.